/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ourgame.tankgame.control;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.ChaseCamera;
import com.jme3.input.FlyByCamera;
import com.jme3.input.InputManager;
import com.jme3.light.AmbientLight;
import com.jme3.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.ourgame.tankgame.barrier.Barrier;
import com.ourgame.tankgame.barrier.BarrierProcess;
import com.ourgame.tankgame.barrier.BrickWall;
import com.ourgame.tankgame.tank.Tank;
import gameutil.Globals;
import gameutil.Role;

/**
 *
 * @author Lcn
 */
public class WorldManagerState extends AbstractAppState {

    private Application app;
    private Node worldNode;
    private AssetManager assetManager;
    private PhysicsSpace physicsSpace;
    private InputManager inputManager;
    private Camera cam;
    private FlyByCamera flyCam;

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        //TODO: initialize your AppState, e.g. attach spatials to rootNode
        //this is called on the OpenGL thread after the AppState has been attached
        super.initialize(stateManager, app);
        this.app = app;
        this.assetManager = app.getAssetManager();
        this.inputManager = app.getInputManager();
        cam = app.getCamera();
        flyCam = ((SimpleApplication) app).getFlyByCamera();

        physicsSpace = stateManager.getState(BulletAppState.class).getPhysicsSpace();
        Node rootNode = ((SimpleApplication) app).getRootNode();
        worldNode = new Node("worldNode");
        rootNode.attachChild(worldNode);

        initControlManager();

        loadLight();
        loadTerrain();
        loadTank();
        loadBrick();
    }

    public WorldManagerState getWorldManagerState() {
        return this;
    }

    public void loadTerrain() {
        /** Load a model. Uses model and texture from jme3-test-data library! */
        RigidBodyControl rbc = new RigidBodyControl(0);
        Spatial terrain = assetManager.loadModel(Globals.TERRAIN_PATH);
        terrain.addControl(rbc);
        worldNode.attachChild(terrain);
        physicsSpace.add(terrain);
    }

    public void loadLight() {
        /** A white ambient light source. */
        AmbientLight ambient = new AmbientLight();
        ambient.setColor(ColorRGBA.White);
        worldNode.addLight(ambient);
        /** A white, directional light source */
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(1, 0, -2).normalizeLocal());
        sun.setColor(ColorRGBA.White);
        worldNode.addLight(sun);
    }

    @Override
    public void update(float tpf) {
        //TODO: implement behavior during runtime
    }

    @Override
    public void cleanup() {
        super.cleanup();
        //TODO: clean up what you initialized in the initialize method,
        //e.g. remove all spatials from rootNode
        //this is called on the OpenGL thread after the AppState has been detached
    }

    public void loadTank() {
        Spatial tankModel = assetManager.loadModel(Globals.TANK_MODEL_PATH);
        ControlSystem baseControlSystem = new BaseControlSystem(assetManager, inputManager, cam);
        baseControlSystem.setPhysicsSpace(physicsSpace);


        Tank kingTigerTank = new Tank(tankModel);
        kingTigerTank.setTankRole(Role.GOOD);
        kingTigerTank.setControlSystem(baseControlSystem);//考虑写到构造方法中
        kingTigerTank.setupModule(); //组装所需的模块
        kingTigerTank.setLocalTranslation(Globals.INIT_POS);//设置位置
        worldNode.attachChild(kingTigerTank);//加入坦克到场景，使坦克可见。
        physicsSpace.add(kingTigerTank);//加入坦克到物理系统。


//        Tank tank1 = new GoodTank(tankModel, Tank.TankRole.BED_TANK);
//        tank1.setLocalTranslation(Globals.INIT_POS);
//        tank1.setBulletModel(assetManager.loadModel(Globals.BULLET_MODEL_PATH));
//        TankControl control = controlManager.getTankControl(tankModel);
//        tank1.setTankControl(control);
//        tank1.getTankControl().setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
//
//        worldNode.attachChild(tank1);
//        physicsSpace.add(tank1);

        /*
         * 暂时放在这里
         */
        ChaseCamera chaseCam = new ChaseCamera(cam, inputManager);
        kingTigerTank.addControl(chaseCam);
        flyCam.setEnabled(false);

    }

    private void loadBrick() {
        for (int i = 0; i < 10; i++) {
            Spatial brick = assetManager.loadModel("Models/brick/box.j3o");
            Barrier b = new BrickWall(brick);
            Vector3f vec = BarrierProcess.processPos(i, new Vector3f(15, 0, 0), b);
            b.setLocalTranslation(vec);
            b.addControl(ControlsManager.getBarrierControl(b));
            physicsSpace.add(b);
            worldNode.attachChild(b);
        }
    }

    private void initControlManager() {
        ControlsManager.setAssetManager(assetManager);
        ControlsManager.setInputManager(inputManager);
        ControlsManager.setCam(cam);
    }
}
